package Servlet;

import DAO.BillDAO;
import DAO.CustomerDAO;
import DAO.DAOFactory;
import javabean.Bill;
import javabean.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;


@WebServlet("/Recharge")
public class RechargeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取充值金额
        resp.setContentType("text/html; charset=UTF-8");
        String rechargeAmount = req.getParameter("recharge");
        String otherAmount = req.getParameter("other-amount");
        // 获取当前用户信息（假设已经通过其他方式获取到了当前用户的信息）
        HttpSession session = req.getSession();
        Customer customer = (Customer) session.getAttribute("loginCustomer");
        BillDAO billDAO = (BillDAO) DAOFactory.getBillDAOInstance();
        Bill bill = new Bill();
        double amount = 0.0;
        PrintWriter out = resp.getWriter();
        // 判断用户选择的充值金额或其他金额
        if (otherAmount != null && !otherAmount.isEmpty()) {
            // 用户输入了其他金额
            amount = Double.parseDouble(otherAmount);
        } else if (rechargeAmount != null && !rechargeAmount.isEmpty()) {
            // 用户选择了预设的充值金额
            amount = Double.parseDouble(rechargeAmount);
        } else {
            // 用户没有选择充值金额
            out.print("<script> alert('请选择充值金额！'); window.location='Recharge.jsp' </script>");
            return;
        }
        // 调用CustomerDAO的充值方法
        CustomerDAO customerDAO = (CustomerDAO) DAOFactory.getCustomerDAOInstance();
        try {
            boolean result = customerDAO.recharge(customer, amount);
            if (result) {
                // 充值成功，跳转到充值成功页面或进行相应处理
                // ...
                bill.setCreatedDate(new java.util.Date());
                bill.setCustomerID(customer.getCustomerID());
                bill.setPaymentMethod("充值");
                bill.setPrice(amount);
                bill.setModifiedDate(new java.sql.Timestamp(new Date().getTime()));
                billDAO.balanceChange(bill);
                out.println("<script> window.alert('充值成功'); window.location.href='Recharge.jsp'; </script>");
            } else {
                // 充值失败，跳转到充值失败页面或进行相应处理
                // ...
                out.print("<script> alert('充值失败！'); window.location='Recharge.jsp' </script>");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            // 充值过程中出现异常，跳转到错误页面或进行相应处理
            // ...
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
